package com.base.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.Users;
import com.base.po.voip.VersionHistory;
import com.base.service.LogsService;
import com.base.service.UsersService;
import com.base.service.VersionHistoryService;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_fabugongnengjieshao {
	@Autowired
	private VersionHistoryService versionHistoryService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private LogsService logsService;// log日志
	
	// 用户首页
	@RequestMapping("/content_fabugongnengjieshao")
	public ModelAndView index(HttpServletRequest request,String type) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		request.setAttribute("type", type);
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/content_fabugongnengjieshao");
		return modelAndView;
	}
	
	String SUCCESS = "success";
	//添加
	@RequestMapping(value = "/insertVersionHistory", method = RequestMethod.POST)
	public void insertVersionHistory(HttpServletRequest request,HttpServletResponse response,
			String version,String content,Byte type) throws Exception {
		// 返回ModelAndView
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}	
		
	    Users u = usersService.selectByPrimaryKey(adminId);
	    
		VersionHistory versionHistory = new VersionHistory(Produce.getUUID(),adminId,Produce.getNowDate(),
				version,content,type);
		
		int resultCode = versionHistoryService.insert(versionHistory);
		
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_UPDATE,u.getUsername()+"添加1条版本介绍", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE,u.getUsername()+"添加版本介绍失败", usernamesys,
					true, IP.getIpAddr(request));
			out.print("添加失败，请您重试");
		}
	}
}
